//
//  YLWorkViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/3/27.
//  Copyright © 2018年 she1323. All rights reserved.
//


#import "LKWorkViewController.h"
#import "LKWorkOrderListTableViewController.h"
#import "LKWorkCustomViewController.h"
#import "LKNetDataManager.h"
#import "LKSearchBar.h"
#import "XXPageTabView.h"
#import "LKWorkOrderDetailViewController.h"
#import "LKContinuInvestViewController.h"
#import "LKWorkCustomerDetailViewController.h"
#import "LKAddWorkCustomViewController.h"
#import "LKSearchResultViewController.h"
@interface LKWorkViewController ()<SegmentTapViewDelegate,FlipTableViewDelegate,OrderPushProtocolDelegate,customPushProtocolDelegate,UISearchBarDelegate,UISearchControllerDelegate,UISearchResultsUpdating>

@property(nonatomic,strong) LKSearchBar *searchBar;
@property(nonatomic,strong) UIButton *suspendBtn;
@property (nonatomic, strong) XXPageTabView *pageTabView;
@property (nonatomic,retain) UISearchController *searchController;
@property(nonatomic,strong)LKWorkOrderListTableViewController *list1;
@property(nonatomic,strong)LKWorkCustomViewController *list2;

@end

@implementation LKWorkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSegmentControlView];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
-(void)setNavgationBackBtn{
    [self setBarButtonItem];
}
- (void)setBarButtonItem
{
    //隐藏导航栏上的返回按钮
    [self.navigationItem setHidesBackButton:YES];
    //用来放searchBar的View
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, WIDTH - 16, 30)];
    titleView.layer.cornerRadius = 8;
    titleView.layer.masksToBounds = YES;
//    titleView.backgroundColor = COLOR_HEX(0xEDF2F6);
    //创建searchBar
//   LKSearchBar *searchBar = [[LKSearchBar alloc] initWithFrame:CGRectMake(0, 0, titleView.bounds.size.width, titleView.bounds.size.height)];
    LKSearchBar *searchBar = [[LKSearchBar alloc] initWithFrame:CGRectMake(0, 0, titleView.bounds.size.width , titleView.bounds.size.height)];
    
    //默认提示文字
    searchBar.placeholder = @"输入客户姓名或合同编号";
    searchBar.delegate = self;
//    searchBar.showsCancelButton = YES;
    UIButton *cancel = [searchBar valueForKey:@"_cancelButton"];

    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [titleView addSubview:searchBar];
    self.navigationItem.titleView = titleView;
    self.searchBar = searchBar;
//    [self setsearchVcinView];
}
-(void)setsearchVcinView{
    //创建UISearchController
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    //设置代理
    self.searchController.delegate= self;
    self.searchController.searchResultsUpdater = self;
    
    //包着搜索框外层的颜色
    self.searchController.searchBar.barTintColor = [UIColor yellowColor];
    
    //提醒字眼
    self.searchController.searchBar.placeholder= @"请输入关键字搜索";
    
    //提前在搜索框内加入搜索词
//    self.searchController.searchBar.text = @"我是周杰伦";
    
    
    //设置UISearchController的显示属性，以下3个属性默认为YES
    
    //搜索时，背景变暗色
    self.searchController.dimsBackgroundDuringPresentation = NO;
    //搜索时，背景变模糊
    //    self.searchController.obscuresBackgroundDuringPresentation = NO;
    
    //点击搜索的时候,是否隐藏导航栏
        self.searchController.hidesNavigationBarDuringPresentation = NO;
    
    //位置
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44);
    
    // 添加 searchbar 到 headerview
    self.navigationItem.titleView = self.searchController.searchBar;
    
#warning 如果进入预编辑状态,searchBar消失(UISearchController套到TabBarController可能会出现这个情况),请添加下边这句话
    self.definesPresentationContext = YES;
    
}
-(void)setSegmentControlView{
    
    LKBaseLineView *topLine = [LKBaseLineView setLineView];
    topLine.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, 1);
    [self.view addSubview:topLine];
    self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(8,NAVIGATION_BAR_HEIGHT + 9, WIDTH - 16, 27) withDataArray:[NSArray arrayWithObjects:@"订单",@"客户", nil] withFont:14];
    
    self.segment.delegate = self;
    self.segment.lineColor = [UIColor clearColor];
    self.segment.titleFont = 14;
    self.segment.textNomalColor = COLOR_HEX(0x4A78E8);
    self.segment.textSelectedColor = [UIColor whiteColor];
    self.segment.layer.borderColor = COLOR_HEX(0x4A78E8).CGColor;
    self.segment.layer.borderWidth = 1;
    self.segment.layer.cornerRadius = 8;
    self.segment.layer.masksToBounds = YES;
    [self.view addSubview:self.segment];
    self.list1 = [[LKWorkOrderListTableViewController alloc] init];
    _list1.delegate = self;
   self.list2 = [[LKWorkCustomViewController alloc] init];
    _list2.delegate = self;
    LKBaseLineView *bottomLine = [LKBaseLineView setLineView];
    bottomLine.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT + 44, WIDTH, 1);
    [self.view addSubview:bottomLine];
    self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 45, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 45 + TAB_BAR_HEIGHT)) withArray:@[_list1,_list2]];
    self.flipView.delegate = self;
    [self.view addSubview:self.flipView];
    self.suspendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.suspendBtn setBackgroundImage:[UIImage imageNamed:@"yuan"] forState:UIControlStateNormal];
    [self.suspendBtn setTitle:@"续投" forState:UIControlStateNormal];
    [self.suspendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.suspendBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [self.view addSubview:self.suspendBtn];
    [self.suspendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.mas_equalTo(self.view.mas_right).mas_offset(-10);
        make.bottom.mas_equalTo(self.view).mas_offset(-80 - TAB_BAR_HEIGHT);
    }];
    self.suspendBtn.titleEdgeInsets = UIEdgeInsetsMake(-10, 1, 10, -1);
    [self.view bringSubviewToFront:self.suspendBtn];
    WS(weakSelf)
    [[self.suspendBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIButton *btn = (UIButton *)x;
        if ([btn.titleLabel.text  isEqual: @"续投"]) {
            LKContinuInvestViewController *vc = [[LKContinuInvestViewController alloc] init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }else{
            LKAddWorkCustomViewController *vc = [[LKAddWorkCustomViewController alloc] init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
            NSLog(@"添加客户");
        }
    }];
    
}
#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    [self.flipView selectIndex:index];
    if (index == 0) {
        [self.suspendBtn setTitle:@"续投" forState:UIControlStateNormal];
        self.searchBar.placeholder = @"输入客户姓名或合同编号";
    }else{
        [self.suspendBtn setTitle:@"添加客户" forState:UIControlStateNormal];
        self.searchBar.placeholder = @"输入客户姓名";
    }
}

-(void)scrollChangeToIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    [self.segment selectIndex:index];
    if (index == 1) {
        [self.suspendBtn setTitle:@"续投" forState:UIControlStateNormal];
         self.searchBar.placeholder = @"输入客户姓名或合同编号";
    }else{
        [self.suspendBtn setTitle:@"添加客户" forState:UIControlStateNormal];
        self.searchBar.placeholder = @"输入客户姓名";
    }
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
   searchBar.showsCancelButton = YES;
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"SearchButton");
    if ([self.suspendBtn.titleLabel.text isEqual:@"续投"]) {
        [self.list1 getDataListWithParam:@{@"PageIndex":@"1",@"MaxResultCount":@"30",@"SearchNameOrCode":searchBar.text}];
    }else{
        
        NSDictionary *param = @{@"PageIndex":@"1",@"MaxResultCount":@"30",@"SearchCustomerName":searchBar.text};
        [self.list2 getDataListWithParam:param];
    }
    NSLog(@"%@",searchBar.text);
    [searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
//    [searchBar setShowsCancelButton:NO animated:YES];
    searchBar.showsCancelButton = NO;
    searchBar.text = @"";
    if ([self.suspendBtn.titleLabel.text isEqual:@"续投"]) {
        [self.list1 getDataListWithParam:@{@"PageIndex":@"1",@"MaxResultCount":@"30",@"SearchNameOrCode":searchBar.text}];
    }else{
        
        NSDictionary *param = @{@"PageIndex":@"1",@"MaxResultCount":@"30",@"SearchCustomerName":searchBar.text};
        [self.list2 getDataListWithParam:param];
    }
    [searchBar resignFirstResponder];

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//       [searchBar resignFirstResponder];
}

#pragma mark -子控制器的push效果代理
-(void)orderPushVc:(UIViewController *)vc{
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)customPushVc:(UIViewController *)vc{
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//谓词搜索过滤
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    //    //修改"Cancle"退出字眼,这样修改,按钮一开始就直接出现,而不是搜索的时候再出现
    //    searchController.searchBar.showsCancelButton = YES;
    //    for(id sousuo in [searchController.searchBar subviews])
    //    {
    //
    //        for (id zz in [sousuo subviews])
    //        {
    //
    //            if([zz isKindOfClass:[UIButton class]]){
    //                UIButton *btn = (UIButton *)zz;
    //                [btn setTitle:@"搜索" forState:UIControlStateNormal];
    //                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //            }
    //
    //
    //        }
    //    }
    
    NSLog(@"updateSearchResultsForSearchController");
//    NSString *searchString = [self.searchController.searchBar text];
//    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
//    if (self.searchListArry!= nil) {
//        [self.searchListArry removeAllObjects];
//    }
//    //过滤数据
//    self.searchListArry= [NSMutableArray arrayWithArray:[self.dataListArry filteredArrayUsingPredicate:preicate]];
//    //刷新表格
//    [self.skTableView reloadData];
    
}


#pragma mark - UISearchControllerDelegate代理
//测试UISearchController的执行过程

- (void)willPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"willPresentSearchController");
}

- (void)didPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"didPresentSearchController");
    
    //    [self.view addSubview:self.searchController.searchBar];
}

- (void)willDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"willDismissSearchController");
}

- (void)didDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"didDismissSearchController");
}

- (void)presentSearchController:(UISearchController *)searchController
{
    NSLog(@"presentSearchController");
}
///** 取消searchBar背景色 */
//- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
//{
//    CGRect rect = CGRectMake(0, 0, size.width, size.height);
//    UIGraphicsBeginImageContext(rect.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//
//    CGContextSetFillColorWithColor(context, [color CGColor]);
//    CGContextFillRect(context, rect);
//
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//
//    return image;
//}
@end
